import api from '../../../http/api';
import CustomNavBar from '@/components/CustomNavBar/CustomNavBar.vue';
	export default {
		components: { CustomNavBar },
		data() {
			return {
				// 我的地址 (固定值)
				myAddress: '',
				
				formData: {
					amount: '',
					password: ''
				},
				
				errors: {
					amount: '',
					password: ''
				},
				
				username:'',//账号
			}
		},
		computed: {
			isFormValid() {
				return Object.values(this.errors).every(error => !error) &&
					Object.values(this.formData).every(val => val !== '');
			}
		},
		onLoad(option) {
			if(option && option.username){
				this.username = option.username;
				this.myAddress = option.wallet_address;
			}
			//this.loadData(); // 首次加载数据
		},
		methods: {
			// 验证金额
			validateAmount() {
				const amount = this.formData.amount;
				if (!amount) {
					this.errors.amount = '金额不能为空';
				} else if (isNaN(amount) || parseFloat(amount) <= 0) {
					this.errors.amount = '请输入有效金额';
				} else if (parseFloat(amount) > 1000000) {
					this.errors.amount = '单笔转账限额1,000,000';
				} else if (!/^\d+(\.\d{1,8})?$/.test(amount)) {
					this.errors.amount = '最多保留8位小数';
				} else {
					this.errors.amount = '';
				}
			},
			
			// 验证交易密码
			validatePassword() {
				const password = this.formData.password;
				if (!password) {
					this.errors.password = '密码不能为空';
				} else {
					this.errors.password = '';
				}
			},
			
			submitForm() {
				// 提交前的最终验证
				this.validateAmount();
				this.validatePassword();
				if (this.isFormValid) {
					
					// 实际提交逻辑
					api.appropriate({
						money:this.formData.amount,
						pay_password:this.formData.password,
						wallet_address:this.myAddress
					}).then(res=>{
						if(res.code==1){
							uni.showToast({title: '转账申请提交成功',icon: 'success'});
							setTimeout(()=>{
								uni.navigateBack({delta:1})
							},1500)
						}else{
							uni.showToast({title: res.msg,icon: 'none'});
						}
					})
				} else {
					uni.showToast({
						title: '请按规定输入',
						icon: 'none'
					});
				}
			}
		}
	}